﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity.DSJCYD94;
using System.Data;
using Microsoft.SharePoint;

namespace JM.JCY.SharePointComponent.CaseManagement.CaseWarningDeptHome
{
    public partial class CaseWarningDeptHomeUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["DeptId"] != null)
                {

                    if (!IsPostBack)
                    {
                        using (UserManagementProxy proxy = new UserManagementProxy())
                        {
                            //var dt = proxy.GetDeptById(Int32.Parse(Request.QueryString["DeptId"].ToString()), string.Empty);
                            //string deptName = dt[0].DeptName;
                            //if (dt.Rows.Count > 0)
                            //{
                            //    GridViewDataBind("", deptName);
                            //}

                        }
                    }
                }
            }
        }

        protected void lbViewAllDiscussion_Click(object sender, EventArgs e)
        {
            Response.Redirect(SPContext.Current.Site.ServerRelativeUrl + "CaseManagementPages/CaseWarningForDept.aspx?DeptId=" + Request.QueryString["DeptId"]);
        }

        protected void gvCaseWarning_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int count = Int32.Parse(e.Row.Cells[3].Text);

                if (count > 0)
                {
                    for (int i = 0; i < e.Row.Cells.Count; i++)
                    {
                        e.Row.Cells[i].ControlStyle.ForeColor = System.Drawing.Color.Red;
                    }
                }
                if (count < 1 && count > -6)
                {
                    for (int i = 0; i < e.Row.Cells.Count; i++)
                    {
                        e.Row.Cells[i].ControlStyle.ForeColor = System.Drawing.Color.Yellow;
                    }
                }

            }
        }

        private void GridViewDataBind(string CBR, string DeptName)
        {
            using (DSJCYD94Proxy proxy = new DSJCYD94Proxy())
            {

                string startDate = "1900-01-01";
                string endDate = "8900-01-01";

                T_AJGL_AJJK_YJAJDataSet.T_AJGL_AJJK_YJAJDataTable dt = proxy.GetAJGL_AJJK_YJAJ(CBR, DeptName, 0, 0, startDate, endDate);
                if (dt.Rows.Count > 0)
                {
                    this.NoRecord.Visible = false;
                    foreach (var item in dt)
                    {
                        item.N_CQTS = (DateTime.Now.Date - item.D_DQRQ.Date).Days.ToString();
                        item.AcceptChanges();
                    }

                    DataView dv = dt.DefaultView;
                    DataTable dtDis = dv.ToTable(true, "C_AH", "C_CBBM", "C_CBR", "N_CQTS");
                    DataTable dt1 = dtDis.Clone();
                    if (dtDis.Rows.Count > 6)
                    {
                        for (int i = 0; i < 6; i++)
                        {
                            DataRow dr = dt1.NewRow();
                            dr["C_AH"] = dtDis.Rows[i]["C_AH"];
                            dr["C_CBBM"] = dtDis.Rows[i]["C_CBBM"];
                            dr["C_CBR"] = dtDis.Rows[i]["C_CBR"];
                            dr["N_CQTS"] = dtDis.Rows[i]["N_CQTS"];
                            dt1.Rows.Add(dr);
                            dt1.AcceptChanges();
                        }
                    }

                    this.gvCaseWarning.DataSource = dt1;
                    this.gvCaseWarning.DataBind();
                }

                else
                {
                    this.NoRecord.Visible = true;
                    this.lbViewAllDiscussion.Visible = false;
                }
             
            }
        }
    }
}
